<template>
	<form class="form-box">
	  <div class="create-puestion common-content-style">
	  	<h3 v-if="currentType=='add'">创建问题</h3>
	  	<h3 v-if="currentType=='modify'">修改问题</h3>
	  	<h3 v-if="currentType=='lookOver'">查看问题</h3>
	  	<div>
				<span><span>*</span>问题标题</span>
				<span style="width: 500px;" :title="question.question">
					<input class="input" v-model="question.question"  name="question" required style="width: 300px;"/>
				</span>
	  	</div>
			<div>
				<span><span>*</span>关键词</span>
				<span style="width: 500px;" :title="question.key1">
					<input class="input" v-model="question.key1"  name="key1" required style="width: 300px;"/>
				</span>
			</div>
			<div>
				<span class="question-type"><span>*</span>问题类型</span>
				<Multi :optionsdata="optionsdata" :selecteddata="selecteddata" @item="chooseData" :initialization="'请选择问题类型'" :displayname="'value'"></Multi>
				<span :class="type?'hide-input':'error'" class="question-type">请选择问题类型</span>
			</div>
			<div>
				<span style="width: 90px;margin-left: 10px;">是否置顶</span>
				<select class="select" v-model="question.istop" >
					<option value="1">是</option>
					<option value="0">否</option>
				</select>
			</div>
			<div>
				<span><span>*</span>解答</span>
				<span :class="answer?'hide-input':'error'">请输入解答</span>
			</div>
	   
	    <div class="editor-container">
	      <UE :defaultMsg=defaultMsg :config=config ref="ue"></UE>
	    </div>
	    <p class="btn">
	    	<input type="submit"  class="Public-btn" value="保存"  v-if="currentType!='lookOver'">
	    	<input class="Public-btn" value="返回" @click="back()" v-if="currentType=='lookOver'">
	    </p> 
	  </div>
	</form>  
</template>
<script>
	import UE from "../shareComponent/UE";
	import Multi from "../shareComponent/multiSelect";
	export default {
    name:'create-puestion',
    data: function(){
      return{
        defaultMsg: "",
        config: {
          initialFrameWidth: null,
          initialFrameHeight: 350
        },
        /*列表全部数据*/
        optionsdata:[],
        /*选中数据列表*/
        selecteddata:[],
        /*问题*/
       	question:{
       		id:"",
       		question:"",
       		key1:"",
       		type:"",
       		createBy:"",
       		/*多选下拉选中数组*/
       		typeArr:[],
       		topEndAt:"",
       		answer:"",
       		status:"",
       		istop:"1"
	      },
	      /*解答*/
      	answer:"true",
      	/*类型*/
      	type:"true",
      	/*当前页码是新增/修改/查看*/
      	currentType:"",
      	/*修改时，是否修改置顶*/
      	changeTop:-1,
      }
    },
    mounted:function(){
    	$(".show-select-name").css({"height": "36px","border":"1px solid #ccc","width": "300px"});
    	$("div.all-option").css("width","300px");
    	this.queryHotList();
    	var  _this= this;
      $.validator.setDefaults({
        submitHandler: function() {
        	/**校验成功后通常调接口**/
        	_this.submitBtn();
        }
      });
			/**校验规则**/
      this.validator = $(".form-box").validate({
        rules:{
          question:{
            required:true,
            maxlength: 100
          },
          key1:{
            required:true,
            maxlength: 50
          },
          articleTitle:{
            required:true,
          },
          articleTitle:{
            required:true,
          },
        },
        messages:{
          question:{
            required:"请输入问题标题"
          },
          key1:{
            required:"请输入关键词"
          },
          articleTitle:{
            required:"请输入标题"
          },
          articleTitle:{
            required:"请输入标题"
          },
        }
      });
      /*当前页面是新增，修改，查看页面*/
      this.currentType=this.$route.query.currentType;
      /*获取回显数据*/
     	if(this.$route.query.currentType=="modify"||this.$route.query.currentType=="lookOver"){
     		this.queryQuestionById();
     	}
     	$("#edui88_bottombar").css("display","none");
    },
    components:{UE,Multi},
    methods:{
      /*返回*/
      back:function(type){
        this.$router.push({path: '/main/questionBank?status='+this.$route.query.status});
    	},
      /*下拉多选选中的数据*/
	    chooseData:function(data){
	     	this.question.typeArr=data;
	    },
	    /*获取数据字典下拉列表*/
    	queryHotList:function(){
	   		this.$get('/system/queryDataByParentId/9')
    		.then(response => {
    			/*未登录重定向*/
          if(response==444){
          	this.$router.push({path: '/login'});
          }else{
          	if (response.resultCode ==0) {
	            this.optionsdata=response.resultData;
	            /*for(var i=0;i<this.optionsdata.length;i++){
	            	if(this.optionsdata[i].id==10){
	            		this.optionsdata.splice(i,1);
	            		return;
	            	}
	            }*/
	          }else{
	          	layer.alert(response.resultMsg);
	          }
          }
         
        }).catch(err => {
          console.log(err);
        });
	   	},
	   	/*回显数据*/
	   	queryQuestionById:function(){
	   		this.$post(
	   			'/qa/queryQuestionById',
	   			{id:this.$route.query.id}
	   		)
    		.then(response => {
    			/*未登录重定向*/
          if(response==444){
          	this.$router.push({path: '/login'});
          }else{
          	if (response.resultCode ==0) {
	            this.question={
			       		id:response.resultData.id,
			       		question:response.resultData.question,
			       		key1:response.resultData.key1,
			       		type:"",
			       		/*多选下拉选中数组*/
			       		typeArr:response.resultData.typeList,
			       		topEndAt:response.resultData.topendat,
			       		answer:response.resultData.answer,
			       		status:response.resultData.status,
				      };
				      this.defaultMsg=response.resultData.answer;
				      if(Date.parse(new Date())<response.resultData.topendat){
				      	this.question.istop=1;
				      	this.changeTop=1;
				      }else{
				      	this.question.istop=0;
				      	this.changeTop=0;
				      }
				      this.selecteddata=response.resultData.typeList;
				      
	          }else{
	          	layer.alert(response.resultMsg);
	          }
          }
         
        }).catch(err => {
          console.log(err);
        });
	   	},
	   	/*保存或者发布文件*/
      submitBtn:function(){
      	this.type=this.question.typeArr.length;
      	let content = this.$refs.ue.getUEContent();
        this.question.answer=content;
        this.answer=content;
    		if(!this.type||!this.answer){
      		return;
      	}
    		this.question.type="";
      	for(var i=0;i<this.type;i++){
      		this.question.type+=this.question.typeArr[i].id+",";
      	}
      	this.question.type=this.question.type.substring(0,this.question.type.length-1);
				
				this.question.status=this.$route.query.status;
				if(this.changeTop!=this.question.istop){
					var timeStamp=Date.parse(new Date());
					if(this.question.istop!=0){
						this.question.topEndAt=timeStamp+ 86400 * 7*1000;
					}else{
						this.question.topEndAt=timeStamp;
					}
				}
				this.question.topEndAt=this.formatTime(this.question.topEndAt);
        delete this.question.typeArr;
        if(this.currentType=="add"){
        	this.question.id=0;
        	this.question.pageView=0;
        }else{
        	this.question.id=this.$route.query.id;
        }
      	this.$post('/qa/addQuestion',this.question)
    		.then(response => {
    			/*未登录重定向*/
          if(response==444){
          	this.$router.push({path: '/login'});
          }else{
          	if (response.resultCode ==0) {
          		this.$router.push({path: '/main/questionBank?status='+this.$route.query.status});
	          }else{
	          	layer.alert(response.resultMsg);
	          }
          }
         
        }).catch(err => {
          console.log(err);
        })
      },
    }
	}
</script>
<style lang="less" scoped>
	.create-puestion{
		div.multi{
			width: 300px;
			display: inline-block;
		}
		.question-type{
			position: relative;
    	top: -12px;
		}
		>p.btn{
			>input{
				width: 70px;
				text-align: center;
			}
		}
		.input,.select{
			margin-right: 20px;
		}
		.question-type{
			position: relative;
	    top: -12px;
	   /* left: 10px;*/
		}
	}
</style>
